<template>
  <VPage class="app-container">
    <VTable ref="tableRef" v-bind="tableOptins">
      <template #form="{ form }">
        <VGroup>
          <el-select class="selectClass" style="max-width: 100px" v-model="form.orderRange.type" placeholder="请选择">
            <el-option v-for="item in orderOption" :key="item.value" :label="item.label" :value="item.value"></el-option>
          </el-select>
          <el-input v-model="form.orderRange.value" placeholder="请输入" class="w-40"></el-input>
        </VGroup>
        <multiInput v-model="form.sku" placeholder="SKU" class="w-50"></multiInput>

        <el-select class="selectClass" v-model="form.status" placeholder="状态" multiple  collapse-tags>
          <el-option v-for="item in options['waremanage.productBorrowSampleOrder']?.[0].productBorrowSampleOrderStatus"
            :key="item.value" :label="item.label" :value="item.value"></el-option>
        </el-select>
        <VGroup>
          <el-select class="selectClass" v-model="form.timeRange.type" style="max-width: 120px">
            <el-option v-for="item in timeOption" :key="item.value" :label="item.label" :value="item.value"></el-option>
          </el-select>
          <el-date-picker v-model="form.timeRange.value" type="daterange" range-separator="至" start-placeholder="开始时间"
            end-placeholder="结束时间" value-format="YYYY-MM-DD HH:mm:ss" :default-time="defaultTime()" style="max-width: 300px" />
        </VGroup>
      </template>
      <template #toolbar_btns>
        <el-button type="primary" @click="open('add')"><svg-icon class="text-white mr-1" icon-class="add"/>新增</el-button>
        <el-button type="" @click="handleBusinessVerify">业务审核</el-button>
        <el-button type="" @click="handleWarehouseVerify">仓库审核</el-button>
        <el-button type="" @click="handleCancel()">取消</el-button>
        <el-button type="" @click="handleEnd">手动完结</el-button>
        <el-button class="ml-auto" @click="handleExport"><svg-icon icon-class="import" />导出</el-button>
      </template>
    </VTable>
    <ComModal ref="ComModalRef" @query="query"></ComModal>
    <derived-center ref="DerivedCenterRef"></derived-center>

  </VPage>
</template>
<script setup name="InventoryProduct-borrow-sample-order" lang="jsx">
import { ElMessage, ElMessageBox } from 'element-plus'
import { ref, reactive, toRefs } from 'vue'
import { getEnums } from '@/api/public'
import { queryProductBorrowList, businessVerify, warehouseVerify, endProductBorrow, cancelProductBorrow } from './api'
import { getWareData } from '@/api/public'
import ComModal from './components/ComModal.vue'
import multiInput from '@/components/multiInput'


const timeOption = [
  { label: "创建时间", value: "create_time" },
  { label: "业务审核时间", value: "op_time" },
  { label: "仓库审核时间", value: "pick_complete_time" },
  { label: "确认时间", value: "out_warehouse_time" },
]

const orderOption = [
  { label: "借样单", value: 'product_borrow_sample_order_no' },
  { label: "审批单号", value: 'verify_order_no' },
]

const tableRef = ref()
const tableOptins = reactive({
  rowConfig: { height: 60 },
  formConfig: {
    data: {
      orderRange: { type: 'product_borrow_sample_order_no', value: [], range: true },
      timeRange: { type: 'create_time', value: [], range: true },
    }
  },
  columns: [
    { type: 'checkbox', width: "40" },
    {
      type: "seq", title: '序号', width: 80
    },
    {
      field: 'product_borrow_sample_order_no', title: '借样单', width: 200
    },
    {
      field: 'verify_order_no', title: '审批单号'
    },
    {
      field: 'warehouse_name', title: '仓库名称'
    },
    {
      field: 'status_cn', title: '状态'
    },
    {
      field: 'create_user', title: '操作人'
    },
    {
      field: 'create_time', title: '操作日期'
    },
    {
      field: 'create_user', title: '创建人'
    },
    {
      field: 'create_time', title: '创建日期'
    },
    {
      title: '操作', width: "100", fixed: "right", slots: {
        default: ({ row }) => {
          return <div class="table-option">
            <div>
              {(row.status == 3 || row.status == 4) && <el-link type="primary" underline={false} class="mr-2" onClick={() => open('out', row.id)}>出库</el-link>}
              {row.status == 1 && <el-link type="primary" underline={false} class="mr-2" onClick={() => open('edit', row.id)}>编辑</el-link>}
              {(row.status == 4 || row.status == 5 || row.status == 6) && <el-link type="primary" underline={false} class="mr-2" onClick={() => open('back', row.id)}>归还</el-link>}
              <el-link type="primary" underline={false} class="mr-2" onClick={() => open('view', row.id)}>查看</el-link>
            </div>
          </div >
        }
      }
    }
  ],
  proxyConfig: {
    seq: true,
    props: {
      result: 'data',
      total: 'total',
    },
    ajax: {
      query: async ({ page, form }) => {
        const res = await queryProductBorrowList({ ...form, ...page })
        return new Promise(function (resolve) {
          resolve({
            data: res.data,
            total: res.total
          })
        })
      }
    }
  }
})
const query = () => {
  tableRef.value.query()
}

const data = reactive({
  options: []
})
const { options } = toRefs(data)

// 获取枚举类
const fetchOption = async () => {
  getEnums({
    version: "v1", "name": [
      "waremanage.productBorrowSampleOrder"
    ]
  }).then(res => {
    options.value = res.data
  })
}
provide('options', options)
fetchOption()

const ComModalRef = ref()
const open = (type, id) => {
  ComModalRef.value.open(type, id)
}

const handleBusinessVerify = () => {
  const records = getSelectRow().map(i => {
    return i.id
  })
  if (records.length == 0) {
    ElMessage.error('请先选择行')
    return
  }
  ElMessageBox.confirm('是否取消当前勾选项?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    businessVerify({ ids: records })
      .then((res) => {
        query()
        ElMessage.success(res.message)
      })
  })
}
const handleWarehouseVerify = () => {
  const records = getSelectRow().map(i => {
    return i.id
  })
  if (records.length == 0) {
    ElMessage.error('请先选择行')
    return
  }
  ElMessageBox.confirm('是否取消当前勾选项?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    warehouseVerify({ ids: records })
      .then((res) => {
        query()
        ElMessage.success(res.message)
      })
  })
}
const handleEnd = () => {
  const records = getSelectRow().map(i => {
    return i.id
  })
  if (records.length == 0) {
    ElMessage.error('请先选择行')
    return
  }
  ElMessageBox.confirm('是否取消当前勾选项?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    endProductBorrow({ ids: records })
      .then((res) => {
        query()
        ElMessage.success(res.message)
      })
  })
}
const handleCancel = (id) => {
  const records = getSelectRow().map(i => {
    return i.id
  })
  if (!id) {
    if (records.length == 0) {
      ElMessage.error('请先选择行')
      return
    }
  }
  const ids = id ? [id] : records
  ElMessageBox.confirm('是否取消当前勾选项?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    cancelProductBorrow({ ids })
      .then((res) => {
        query()
        ElMessage.success(res.message)
      })
  })
}

// 导出
const DerivedCenterRef = ref()
const handleExport = (val) => {
  let condition = tableRef.value.getQueryForm()
  DerivedCenterRef.value.open({
    id: 41,
    tag_name: 'wms',
    condition
  })
}

//获取已选中的行
const getSelectRow = () => {
  return tableRef?.value.$table.getCheckboxRecords()
}



</script>